﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ccit.Utils;
using Ccit.ClassLibrary;

namespace Ccit.SysPlat.Purview
{
    /// <summary>
    /// Description: 系统平台权限菜单 -> 菜单管理业务层
    /// Author: tim 2010-03-02
    /// </summary>
    public class MenuBLL
    {
        #region initialize 

        IMenu _menuProcessor = null;

        public MenuBLL()
        {
            _menuProcessor = Factory.CreateObject(MsMenu.assemblyName, MsMenu.className, ImportComponentType.DotNetDll, null) as IMenu;
        }

        public void Release()
        {
            _menuProcessor = null;
        }
        #endregion

        /// <summary>
        /// 获取第一级菜单集合
        /// </summary>
        /// <returns></returns>
        public List<MsMenu> GetStairMenuList()
        {
            return _menuProcessor.GetStairMenuList();            
        }


        /// <summary>
        /// 获取当前菜单的下一级子菜单集合
        /// </summary>
        /// <param name="menuId">当前菜单Id</param>
        /// <param name="sourceType">页面传入的当前操作的源类型
        /// 0:当前正在通过admin进行菜单管理操作
        /// 1:当前正在通过模块装配功能添加菜单操作 
        /// </param>
        /// <returns></returns>
        public List<MsMenu> GetSubMenuList(string menuId,string sourceType)
        {
            if (string.IsNullOrEmpty(menuId)) return new List<MsMenu>();

            return _menuProcessor.GetSubMenuList(menuId, sourceType);
        }


        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 结果描述
        /// </returns>
        public List<string> Delete(string menuId)
        {
            if (string.IsNullOrEmpty(menuId)) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            List<string> lstResult = _menuProcessor.Delete(menuId);
            return lstResult;
        }

        /// <summary>
        /// 获取从当前菜单开始的所有上级菜单Id串(包含当前菜单)
        /// </summary>
        /// <param name="menuId">当前菜单Id</param>
        /// <returns></returns>
        public string GetMenuLevelStruct(string menuId)
        {
            if (string.IsNullOrEmpty(menuId)) return string.Empty;

            return _menuProcessor.GetMenuLevelStruct(menuId);
        }


        /// <summary>
        /// 按菜单层级结构(即前导缩进)的形式获取所有有效的部门信息
        /// </summary>        
        /// <param name="filterMenuId">过滤掉的菜单Id(同时过滤掉菜单下的所有子菜单)</param>
        /// <param name="sysParam">是否能编辑系统菜单的开关</param>
        /// <param name="bEmptyHeader">是否允许空头</param>
        /// <param name="headerText">空头文本</param>
        /// <returns></returns>
        public List<MsKeyValue> GetALLMenus(string filterMenuId, string sysParam,bool bEmptyHeader,string headerText)
        {
            List<MsKeyValue> lstMenu = _menuProcessor.GetALLMenus(filterMenuId, sysParam);
            if (bEmptyHeader) lstMenu.Insert(0, new MsKeyValue { key = "", val = headerText });
            return lstMenu;
        }

        /// <summary>
        /// 根据菜单Id获取菜单消息对象
        /// </summary>
        /// <param name="menuId">菜单Id</param>
        /// <returns></returns>
        public MsMenu GetMenuInfo(string menuId)
        {
            if (string.IsNullOrEmpty(menuId)) return null;

            return _menuProcessor.GetMenuInfo(menuId);
        }


        /// <summary>
        /// 保存菜单信息
        /// </summary>
        /// <param name="msMenu">菜单信息消息对象</param>
        /// <returns>
        ///     【0】: 操作结果
        ///     【1】: 结果描述
        /// </returns>
        public List<string> Save(MsMenu msMenu,eTrueFalse isCheck)
        {
            if (null == msMenu) return new List<string> { "-999", "操作失败:缺少必要参数!" };

            string menuId = msMenu.menuId;            
            if (!string.IsNullOrEmpty(menuId) && isCheck == eTrueFalse.TRUE)
            {
                List<string> lstResunt = _menuProcessor.IsCanSave(msMenu);
                if (Int32.Parse(lstResunt[0]) <= 0) return lstResunt;
            }
            return _menuProcessor.Save(msMenu);
        }


        

    }
}
